<!-- 加工初检 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
      <el-button icon="el-icon-delete" type="primary" @click="searchReset">清空</el-button>
      <el-button type="primary" @click="checkByBatch">批量初检</el-button>
    </el-button-group>
        <el-form inline label-width="100px" @keyup.enter.native="searchHandle">
          <el-form-item label="配镜单号">
            <el-input v-model="dataForm.saleBillId"></el-input>
          </el-form-item>
          <el-form-item label="顾客姓名">
            <el-input v-model="dataForm.customerName"></el-input>
          </el-form-item>
          <el-form-item label="销售门店">
            <el-select v-model="dataForm.deptId" :disabled="deptType != '2'" clearable>
              <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="取镜门店">
            <el-select v-model="dataForm.takeMirroDeptId"  clearable>
              <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="配镜时间">
            <el-col :span="11">
              <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.saleTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.saleTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-col>
          </el-form-item>
        </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column prop="saleBillId" label="配镜单号">
            <template slot-scope="scope">
              <el-button type="text" @click="checkDetailByOrder(scope.row.saleBillId)">{{scope.row.saleBillId}}</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="customerName" label="顾客姓名"></el-table-column>
          <el-table-column prop="deptName" label="销售门店"></el-table-column>
          <el-table-column prop="takeMirroDeptName" label="取镜门店"></el-table-column>
          <el-table-column prop="createTime" label="配镜时间"></el-table-column>
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" @click="yesOrNoCheck(scope.row.saleId,scope.row.saleBillId)">检验</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
      </div>
    <order-info v-if="infoVisible" ref="infoDialog"></order-info>
  </div>
</template>

<script>
  import { mapGetters } from "vuex"
  import { workCheckFetchList, workCheckCheckObj,workFirstCheckByIds } from "@/api/eims/inspect"
  import { getDeptInfo,getObj,getDeptListByUnitId } from '@/api/admin/dept'
  import OrderInfo from '@/views/eims/optician/glassesinfo/detail'

  export default {
    data() {
      return {
        dataForm: {
          saleStatus: 6,
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        unitId: '',
        deptType: '',
        deptOptions: [],
        selectList: [],
        infoVisible: false
      };
    },
    created() {
      this.getUnit();
    },
    computed: {
      ...mapGetters(["permissions",'userInfo']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    components: {
      OrderInfo
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 清空搜索框
      searchReset() {
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
        this.dataForm.saleStatus = 6
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let params = {current: this.pageIndex, size: this.pageSize}
        workCheckFetchList(Object.assign(params, this.dataForm)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })

      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 是否初检
      yesOrNoCheck(id,saleBillId) {
        this.$confirm("是否确认检验销售单为" + saleBillId +"的订单", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return workCheckCheckObj({
            saleId: id
          });
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success("初检成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      handleSelectionChange(val){
        this.selectList = val
      },
      //  批量初检
      checkByBatch() {
        let ids = []
        this.selectList.forEach(item => {
          ids.push(item.saleId)
        })
        this.$confirm("是否确认批量检验销售订单", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return workFirstCheckByIds({
            saleIds: ids
          })
        }).then(data => {
          if(response.data.code == '0'){
            this.$message.success("初检成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      //  查看配镜单详情
      checkDetailByOrder(order){
        this.infoVisible = true;
        this.$nextTick(() => {
          this.$refs.infoDialog.init('',order,'配镜单详情');
        });
      },
      //  获取部门列表
      getDeptList() {
        getDeptInfo().then(result => {
          this.deptOptions = result.data.data
          this.searchHandle()
        })
      },
      getUnit(){
        getObj(this.userInfo.lessDeptId).then(result => {
          this.unitId = result.data.data.unitId
          this.deptType = result.data.data.deptType
          if(!this.dataForm.deptId && this.deptType != 2){
            getDeptListByUnitId(this.unitId).then(result => {
              this.$set(this.dataForm,'deptId',result.data.data[0].id)
            })
          }
          setTimeout(() => {
            this.getDeptList();
          },100)
        })
      }
    }
  };
</script>
